package com.utopa.canal.client.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * @author authorZhao
 * @date 2020年04月16日
 */
@Slf4j
public class CanalApplicationEvent extends ContextRefreshedEvent implements ApplicationContextAware, ApplicationRunner {
    private static ApplicationContext applicationContext;

    public CanalApplicationEvent(ApplicationContext source) {
        super(source);
    }

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        log.info("项目启动成功，开始发布 CanalApplicationEvent 事件");
        CanalApplicationEvent.applicationContext.publishEvent(this);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CanalApplicationEvent.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> clazz) {
        return CanalApplicationEvent.applicationContext.getBean(clazz);
    }
}
